package com.bl.student.service.biz.user.impl;

import com.bl.student.common.ApiResponseCode;
import com.bl.student.common.CommonServiceException;
import com.bl.student.common.utils.ListUtils;
import com.bl.student.dao.model.UserEntity;
import com.bl.student.dao.redis.UserInfoDao;
import com.bl.student.dao.redis.model.UserInfo;
import com.bl.student.dao.repository.UserRepository;
import com.bl.student.service.biz.user.UserService;
import com.bl.student.service.vo.user.*;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yujingyi on 2018/1/15.
 */
@Service
@Log4j2
public class UserServiceImpl implements UserService {

    final static private int VALID_INVITED_REWARDED_SALIVA = 20;

    @Autowired
    UserRepository userRepository;

    @Autowired
    UserInfoDao userInfoDao;




    @Override
    public UserSimpleInfoVo getUserById(int userId) {
        UserEntity userEntity = userRepository.findOne(userId);
        return null;
    }

    @Override
    public UserEntity getUserByMobile(String mobile) {
        List<UserEntity> userEntityList = userRepository.findByMobile(mobile);
        return ListUtils.getFirst(userEntityList);
    }

    @Override
    public UserEntity getUserByAccount(String account) {
        List<UserEntity> userEntityList = userRepository.findByAccount(account);
        return ListUtils.getFirst(userEntityList);
    }

    @Override
    public UserEntity getUserByNickname(String nickName) {
        List<UserEntity> userEntityList = userRepository.findByNickname(nickName);
        return ListUtils.getFirst(userEntityList);
    }

    @Override
    @Transactional
    public void createUser(UserEntity userEntity) {
        userRepository.save(userEntity);
    }

    @Override
    @Transactional
    public void modifyUser(UserInfoModifyVo userInfoModifyVo, UserInfo userInfo) {
        UserEntity userEntity = userRepository.findOne(userInfo.getId());
        if (userEntity == null) {
            throw new CommonServiceException(ApiResponseCode.DATA_NOT_EXIST);
        }

        userEntity.setNickname(userInfoModifyVo.getNickname());

        userRepository.save(userEntity);
    }

}
